package com.cxs.rdbdemo.slice;

import com.cxs.rdbdemo.ResourceTable;
import com.cxs.rdbdemo.utils.DeviceUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Switch;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {

    private String baseUrl;
    private static final String TABLE_USER = "user";
    private static final String TABLE_BOOK = "book";
    Uri userUri, bookUri;
    DataAbilityHelper helper;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        helper = DataAbilityHelper.creator(this);

        initUri("");
        initView();
    }

    private void initUri(String deviceId) {
        baseUrl = "dataability://" + deviceId + "/com.cxs.rdbdemo.DataAbility/";
        userUri = Uri.parse(baseUrl + TABLE_USER);
        bookUri = Uri.parse(baseUrl + TABLE_BOOK);
    }

    private void initView() {
        findComponentById(ResourceTable.Id_btn_insert_user).setClickedListener(c -> insertUser());
        findComponentById(ResourceTable.Id_btn_query_user).setClickedListener(c -> queryUser());
        findComponentById(ResourceTable.Id_btn_update_user).setClickedListener(c -> updateUser());
        findComponentById(ResourceTable.Id_btn_delete_user).setClickedListener(c -> deleteUser());

        findComponentById(ResourceTable.Id_btn_insert_book).setClickedListener(c -> insertBook());
        findComponentById(ResourceTable.Id_btn_query_book).setClickedListener(c -> queryBook());
        findComponentById(ResourceTable.Id_btn_update_book).setClickedListener(c -> updateBook());
        findComponentById(ResourceTable.Id_btn_delete_book).setClickedListener(c -> deleteBook());

        Switch btnSwitch = (Switch)findComponentById(ResourceTable.Id_switch_device_state);
        btnSwitch.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                if (b) {
                    String deviceId = DeviceUtil.getDeviceId();
                    initUri(deviceId);
                } else {
                    initUri("");
                }
            }
        });

    }

    private void deleteBook() {
        DataAbilityPredicates predicates = new DataAbilityPredicates().lessThan("price", 100.00);
        try {
            helper.delete(bookUri, predicates);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    private void deleteUser() {
        DataAbilityPredicates predicates = new DataAbilityPredicates()
                .greaterThan("age", 18);
        try {
            helper.delete(userUri, predicates);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }


    private void updateBook() {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString("title", "鸿蒙");
        DataAbilityPredicates predicates = new DataAbilityPredicates().equalTo("title", "HarmonyOS");

        try {
            int update = helper.update(bookUri, valuesBucket, predicates);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    private void updateUser() {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString("name", "张三");
        DataAbilityPredicates predicates = new DataAbilityPredicates().equalTo("name", "zhangsan");

        try {
            int update = helper.update(userUri, valuesBucket, predicates);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }


    private void queryBook() {
        DataAbilityPredicates predicates = new DataAbilityPredicates();

        try {
            ResultSet query = helper.query(bookUri,
                    new String[]{"title", "price"}, predicates);
            while (query.goToNextRow()) {
                System.out.println(query.getString(query.getColumnIndexForName("title")));
                System.out.println(query.getDouble(query.getColumnIndexForName("price")));
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }

    }

    private void insertBook() {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString("title", "HarmonyOS");
        valuesBucket.putDouble("price", 67.1);

        try {
            helper.insert(bookUri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    private void queryUser() {
        System.out.println(baseUrl);
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        try {
            ResultSet query = helper.query(userUri,
                    new String[]{"name", "age", "salary"}, predicates);
            while (query.goToNextRow()) {
                System.out.println(query.getString(query.getColumnIndexForName("name")));
                System.out.println(query.getInt(query.getColumnIndexForName("age")));
                System.out.println(query.getDouble(query.getColumnIndexForName("salary")));
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    private void insertUser() {
        ValuesBucket valuesBucket = new ValuesBucket();
        valuesBucket.putString("name", "zhangsan");
        valuesBucket.putInteger("age", 20);
        valuesBucket.putDouble("salary", 567.34);
        try {
            helper.insert(userUri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
